package edu.nepu.flink.api.splitStream;

import edu.nepu.flink.api.bean.WaterSensor;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SideOutputDataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

/**
 * @Date 2024/2/28 22:21
 * @Created by chenshuaijun
 */
public class SideOutputStream {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        DataStreamSource<WaterSensor> elementSource = env.fromElements(
                new WaterSensor("s1", 1L, 1),
                new WaterSensor("s2", 2L, 11),
                new WaterSensor("s3", 4L, 5)
        );
        /**
         * 使用侧输出流的方式进行数据的分流
         *  这里需要使用到底层的api：process
         *
         *  我们的目标是：
         *      将Sensor中id为s1的放到s1SideOutput中
         *      将Sensor中id为s2的放到s2SideOutput中
         *      其余的数据放入到主流中
         */
        OutputTag<WaterSensor> s1OutputTag = new OutputTag<WaterSensor>("s1"){};
        OutputTag<WaterSensor> s2OutputTag = new OutputTag<WaterSensor>("s2"){};
        SingleOutputStreamOperator<WaterSensor> splitStream = elementSource.process(new ProcessFunction<WaterSensor, WaterSensor>() {
            @Override
            public void processElement(WaterSensor value, ProcessFunction<WaterSensor, WaterSensor>.Context ctx, Collector<WaterSensor> out) throws Exception {
                if ("s1".equals(value.getId())) {
                    ctx.output(s1OutputTag, value);
                } else if ("s2".equals(value.getId())) {
                    ctx.output(s2OutputTag, value);
                } else {
                    out.collect(value);
                }
            }
        });

        splitStream.print("主流的数据");
        SideOutputDataStream<WaterSensor> sideOutputStream1 = splitStream.getSideOutput(s1OutputTag);
        SideOutputDataStream<WaterSensor> sideOutputStream2 = splitStream.getSideOutput(s2OutputTag);
        sideOutputStream1.print("s1");
        sideOutputStream2.print("s2");
        env.execute();
    }
}
